package com.sls.web.manager.action.game;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GameSupplyRule;
import com.sls.web.manager.game.pojo.GoodsManageInfo;
import com.sls.web.manager.game.querybean.GameSupplyRuleQuery;
import com.sls.web.manager.game.service.GameSupplyRuleService;
import com.sls.web.manager.page.Pagination;

/**
 * 游戏供货规则：列表展示，新增，修改，明细
 * 
 * @author gesx
 * 
 */
public class GameSupplyRuleAction extends SuperAction {
	private static final long serialVersionUID = -8352839969058889105L;
	@Resource(name = "gameSupplyRuleService")
	private GameSupplyRuleService gameSupplyRuleService;
	private GameSupplyRule gameSupplyRule;
	private GameSupplyRuleQuery gameSupplyRuleQuery;
	private Pagination<?> page;
	private String goodsId;
	private String id;
	private List<GoodsManageInfo> goodsNameList;

	/**
	 * 跳转添加页面，并查询出所有没有供货规则的商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		goodsNameList = gameSupplyRuleService
				.goodsNameList(getCurrentOperator());
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		String goodsPriceDifference = WebTools
				.getParameter("goodsPriceDifference");
		try {
			gameSupplyRule.setGoodsPriceDifference((int) (Double
					.parseDouble(goodsPriceDifference.trim()) * 1000));
			gameSupplyRuleService.save(gameSupplyRule, getCurrentOperator());
			redirectionUrl = "game_supply_rule!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());

		}
		return SUCCESS;
	}

	/**
	 * 列表显示
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (gameSupplyRuleQuery == null) {
				gameSupplyRuleQuery = new GameSupplyRuleQuery();
				gameSupplyRuleQuery.setPageSize(Constants.INIT_PAGESIZE);
				gameSupplyRuleQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String goodsId = gameSupplyRuleQuery.getGoodsId();
			String goodsName = gameSupplyRuleQuery.getGoodsName();
			goodsId = FilterStringSpaces.checkStringHasSpace(goodsId);
			goodsName = FilterStringSpaces.checkStringHasSpace(goodsName);
			gameSupplyRuleQuery.setGoodsId(goodsId);
			gameSupplyRuleQuery.setGoodsName(goodsName);
			page = gameSupplyRuleService.queryList(gameSupplyRuleQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 修改跳转页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		try {
			if (StringUtils.isNotEmpty(id)) {
				gameSupplyRule = gameSupplyRuleService.queryById(id,
						getCurrentOperator());
				if (gameSupplyRule == null) {
					return ERROR;
				}
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		if (StringUtils.isNotEmpty(id)) {
			return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		String goodsPriceDifference = WebTools
				.getParameter("goodsPriceDifference");
		if (StringUtils.isNotEmpty(id)) {
			try {
				gameSupplyRule.setId(id);
				gameSupplyRule.setGoodsPriceDifference((int) (Double
						.parseDouble(goodsPriceDifference.trim()) * 1000));
				gameSupplyRuleService.update(gameSupplyRule,
						getCurrentOperator());
				redirectionUrl = "game_supply_rule!list.htm";
			} catch (ManagerException e) {
				flag = false;
				addActionError(e.getMessage());
			}
		}
		return SUCCESS;
	}

	/**
	 * 详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryById() throws Exception {
		try {
			gameSupplyRule = gameSupplyRuleService.queryById(id,
					getCurrentOperator());

		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "detail";
	}

	public GameSupplyRuleService getGameSupplyRuleService() {
		return gameSupplyRuleService;
	}

	public void setGameSupplyRuleService(
			GameSupplyRuleService gameSupplyRuleService) {
		this.gameSupplyRuleService = gameSupplyRuleService;
	}

	public GameSupplyRule getGameSupplyRule() {
		return gameSupplyRule;
	}

	public void setGameSupplyRule(GameSupplyRule gameSupplyRule) {
		this.gameSupplyRule = gameSupplyRule;
	}

	public GameSupplyRuleQuery getGameSupplyRuleQuery() {
		return gameSupplyRuleQuery;
	}

	public void setGameSupplyRuleQuery(GameSupplyRuleQuery gameSupplyRuleQuery) {
		this.gameSupplyRuleQuery = gameSupplyRuleQuery;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public List<GoodsManageInfo> getGoodsNameList() {
		return goodsNameList;
	}

	public void setGoodsNameList(List<GoodsManageInfo> goodsNameList) {
		this.goodsNameList = goodsNameList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
